package com.taikang.hmp.frame.login.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

import com.ibatis.common.util.IDHelper;
import com.taikang.hmp.frame.common.HDConstant;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.frame.common.tools.IdnoInfoUtil;
import com.taikang.hmp.frame.common.web.session.SessionBean;
import com.taikang.hmp.frame.customer.bean.CustomerInfo;
import com.taikang.hmp.frame.login.service.ILoginService;
import com.taikang.hmp.frame.user.bean.AuthUserInfo;

/** 
 * login service impl
 * author:qrp
 * time:2015年7月20日 下午2:05:47
 */
public class LoginServiceImpl extends BaseServiceImpl implements ILoginService{

	public String saveRegistInfo(Dto dto) {
		String customer_id = null;
		String requestsource = dto.getAsString("requestsource");
		
		//客户查找
		CustomerInfo ci = new CustomerInfo();
		String name = dto.getAsString("name");
		// 身份证号统一为大写X
		String personIdNo = dto.getAsString("idno");
		if (StringUtils.isNotEmpty(personIdNo)) {
			personIdNo = personIdNo.toUpperCase();
			dto.put("idno", personIdNo);
		}	
		CustomerInfo currentCustomer = (CustomerInfo)super.getNewDao().queryForObject("user_space.getCustomer_idno", personIdNo);
		
		//客户基本信息
		ci.setName(name);
		ci.setId_no(personIdNo);
		ci.setBirthday(IdnoInfoUtil.getBirthday(personIdNo));
		ci.setSex(IdnoInfoUtil.getSex(personIdNo));
		ci.setMobile(dto.getAsString("mobile"));
		ci.setSource_cd(requestsource);
		
		//同一客户识别
		if(currentCustomer!=null && StringUtils.isNotEmpty(currentCustomer.getName()) && currentCustomer.getName().equals(name)){
			customer_id = currentCustomer.getCustomer_id();
			ci.setCustomer_id(customer_id);
			super.getNewDao().update("user_space.update_customerInfo", ci);
		}else{
			// 获取客户号seq
			String customerSeq = (String) super.getNewDao().queryForObject("user_space.getCustomerSeq","");
			if(HDConstant.APP_SOURCE_DIABETES.equals(requestsource)){
				customer_id = "DI"+customerSeq;
			}else{
				customer_id = customerSeq;
			}
			
			ci.setCustomer_id(customer_id);
			ci.setIs_init_plan("0");
			super.getNewDao().insert("user_space.insert_bs_customer", ci);
		}
		
		//
		AuthUserInfo aut = new AuthUserInfo();
		String uid = java.util.UUID.randomUUID().toString();
		aut.setUser_id(uid);
		aut.setUser_name(dto.getAsString("username"));
		aut.setUser_pwd(dto.getAsString("password"));
		aut.setCreate_dt(new Date());
		aut.setCustom_id(customer_id);
		aut.setOperate_date(new Date());
		aut.setOperater(uid);
		aut.setSource_cd(dto.getAsString("requestsource"));
		super.getNewDao().insert("user_space.insert_auth_user", aut);
		return uid;
	}
	
	public String saveTaipingRegistInfo(Dto dto,String uid) {
		String customer_id = null;
		String requestsource = dto.getAsString("requestsource");
		
		//客户查找
		CustomerInfo ci = new CustomerInfo();
		String name = uid;
		//客户基本信息
		ci.setName("taiping"+name);
		ci.setId_no("");
		ci.setHeight("170");
		ci.setWeight("80");
		ci.setBirthday("2016-01-01");
		ci.setSex("0");
		//ci.setMobile(dto.getAsString("mobile"));
		ci.setSource_cd(requestsource);
		// 获取客户号seq
		String customerSeq = (String) super.getNewDao().queryForObject("user_space.getCustomerSeq","");
		if(HDConstant.APP_SOURCE_DIABETES.equals(requestsource)){
			customer_id = "DI"+customerSeq;
		}else{
			customer_id = customerSeq;
		}
		
		ci.setCustomer_id(customer_id);
		ci.setIs_init_plan("0");
		super.getNewDao().insert("user_space.insert_bs_customer", ci);
		
		
		//
		AuthUserInfo aut = new AuthUserInfo();
		
		aut.setUser_id(uid);
		aut.setUser_name("taiping");
		//aut.setUser_pwd(dto.getAsString("password"));
		aut.setCreate_dt(new Date());
		aut.setCustom_id(customer_id);
		aut.setOperate_date(new Date());
		aut.setOperater(uid);
		aut.setSource_cd(dto.getAsString("requestsource"));
		super.getNewDao().insert("user_space.insert_auth_user", aut);
		return uid;
	}
	
	public List<AuthUserInfo> getLoginInfo(String loginName) {
		return super.getNewDao().queryForList("user_space.getLoginInfo", loginName);
	}
	public List<AuthUserInfo> getInfo(String userid) {
		return super.getNewDao().queryForList("user_space.getInfo", userid);
	}

	public Dto updateUserPwd(Dto dto) {
		Dto returnInfo = new BaseDto();
		
		CustomerInfo customerInfo = (CustomerInfo)super.getNewDao().queryForObject("user_space.getCustomer_mobile", dto.getAsString("mobile"));
		if(customerInfo == null){
			 returnInfo.put("mobileExist", "0");
			 return returnInfo;
		}
		
		AuthUserInfo info1 = new AuthUserInfo();
		info1.setCustom_id(customerInfo.getCustomer_id());
		info1.setUser_pwd(dto.getAsString("password"));
		try {
			super.getNewDao().update("user_space.update_user_pwd", info1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<AuthUserInfo> info2 = super.getNewDao().queryForList("user_space.authInfo_customerid", customerInfo.getCustomer_id());
		returnInfo.put("mobileExist", "1");
		returnInfo.put("uid", info2.get(0).getUser_id());
		returnInfo.put("username", info2.get(0).getUser_name());
		return returnInfo;
	}

	public SessionBean getCustomerBasicInfo(String userid) {
		SessionBean sessionBean = new SessionBean();
		AuthUserInfo authUserInfo = (AuthUserInfo)super.getNewDao().queryForObject("user_space.authInfo_uid", userid);
		if( authUserInfo==null ){
			throw new TKException("userid:"+userid+"无对应用户信息");
		}
		CustomerInfo customerInfo = (CustomerInfo)super.getNewDao().queryForObject("user_space.getCustomer_uid", userid);
		if( customerInfo==null ){
			throw new TKException("userid:"+userid+"无对应客户信息");
		}
		sessionBean.setAuthUserInfo(authUserInfo);
		sessionBean.setCustomerInfo(customerInfo);
		return sessionBean;
	}

	
	/**
	 * 用户名校验
	 */
	public boolean check_usn_repeat(String user_name) {
		List<?> user_name_info = super.getNewDao().queryForList("user_space.check_usn_repeat", user_name);
		if(user_name_info!=null && user_name_info.size()>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 身份证号码校验
	 */
	public boolean check_idno_repeat(String idno) {
		List<?> id_no_info = super.getNewDao().queryForList("user_space.getCustomer_idno", idno.toUpperCase());
		if(id_no_info!=null && id_no_info.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 手机号码校验
	 */
	public boolean check_mobile_repeat(String mobile) {
		List<?> mobile_info =super.getNewDao().queryForList("user_space.getCustomer_mobile", mobile);
		if(mobile_info!=null && mobile_info.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 验证码获取
	 */
	public String getIdentify() {
		Random random = new Random();
		String result = "";
		for (int i = 0; i < 6; i++) {
			result += random.nextInt(10);
		}
		return result;
	}

	public String saveReg4Doctor(Dto dto) {
		String customer_id = null;
		String requestsource = dto.getAsString("requestsource");
		//客户查找
		CustomerInfo ci = new CustomerInfo();
		//客户基本信息
		ci.setMobile(dto.getAsString("mobile"));
		ci.setSource_cd(requestsource);
	    // 获取客户号seq
		String customerSeq = (String) super.getNewDao().queryForObject("user_space.getCustomerSeq","");
		if(HDConstant.APP_SOURCE_DIABETES.equals(requestsource)){
			customer_id = "DI"+customerSeq;
		}else{
			customer_id = customerSeq;
		}
		ci.setCustomer_id(customer_id);
		ci.setIs_init_plan("0");
		super.getNewDao().insert("user_space.insert_bs_customer", ci);
		//user信息
		AuthUserInfo aut = new AuthUserInfo();
		String uid = java.util.UUID.randomUUID().toString();
		aut.setUser_id(uid);
		aut.setUser_name(dto.getAsString("username"));
		aut.setUser_pwd(dto.getAsString("password"));
		aut.setCreate_dt(new Date());
		aut.setCustom_id(customer_id);
		aut.setOperate_date(new Date());
		aut.setUser_type("3");//0为正常用户;2为健康管理员；3为泰康医生“黑户”
		aut.setOperater(uid);
		aut.setSource_cd(dto.getAsString("requestsource"));
		super.getNewDao().insert("user_space.insert_auth_doctor_user", aut);
		return uid;
	}

	public void updateRegistInfo(Dto paramDto) {
		String requestsource = paramDto.getAsString("requestsource");
		CustomerInfo ci = new CustomerInfo();
		ci.setName(paramDto.getAsString("name"));
		ci.setMobile(paramDto.getAsString("mobile"));
		ci.setSource_cd(requestsource);
		ci.setCustomer_id(paramDto.getAsString("customerid"));
		ci.setIs_init_plan("0");
		ci.setId_no(paramDto.getAsString("idno"));
		ci.setBirthday(IdnoInfoUtil.getBirthday(paramDto.getAsString("idno")));
		ci.setSex(IdnoInfoUtil.getSex(paramDto.getAsString("idno")));
		ci.setMobile(paramDto.getAsString("mobile"));
		ci.setSource_cd(requestsource);
		super.getNewDao().update("user_space.update_customerInfo", ci);
		//user信息
		AuthUserInfo aut = new AuthUserInfo();
		aut.setUser_id(paramDto.getAsString("userid"));
		aut.setUser_name(paramDto.getAsString("username"));
		aut.setUser_pwd(paramDto.getAsString("password"));
		aut.setCreate_dt(new Date());
		aut.setCustom_id(paramDto.getAsString("customerid"));
		aut.setOperate_date(new Date());
		aut.setUser_type("0");//0为正常用户;2为健康管理员；3为泰康医生“黑户”
		aut.setOperater(paramDto.getAsString("userid"));
		aut.setSource_cd(paramDto.getAsString("requestsource"));
		super.getNewDao().update("user_space.update_auth_doctor_user", aut);
	}

	

}
